package com.deyuanyun.pic.settings.service.base.impl;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.domain.base.BizMsgSendWithBLOBs;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.domain.base.BizMsgUserExample;
import com.deyuanyun.pic.settings.mapper.base.BizMsgSendMapper;
import com.deyuanyun.pic.settings.mapper.base.BizMsgUserMapper;
import com.deyuanyun.pic.settings.service.base.BizMsgUserService;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class BizMsgUserServiceImpl implements BizMsgUserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BizMsgUserServiceImpl.class);

    @Autowired
    private BizMsgUserMapper bizMsgUserMapper;

    @Autowired
    private BizMsgSendMapper bizMsgSendMapper;

    private static final Logger logger = LoggerFactory.getLogger(BizMsgUserServiceImpl.class);

    public BizMsgUser selectByPrimaryKey(String id) {
        return this.bizMsgUserMapper.selectByPrimaryKey(id);
    }

    public int deleteByPrimaryKey(String id) {
        return this.bizMsgUserMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(BizMsgUser record) {
        return this.bizMsgUserMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(BizMsgUser record) {
        return this.bizMsgUserMapper.updateByPrimaryKey(record);
    }

    public int insert(BizMsgUser record) {
        return this.bizMsgUserMapper.insert(record);
    }

    public List<BizMsgUser> queryMsgUserByUser() {
        UserSessionVO userSessionVO = SessionCacheSupport.getSessionVO();
        BizMsgUserExample bizMsgUserExample = new BizMsgUserExample();
        bizMsgUserExample.setDistinct(false);
        bizMsgUserExample.createCriteria().andDeletedEqualTo((short)0).andUserIdEqualTo(userSessionVO.getId());
        return bizMsgUserMapper.selectByExample(bizMsgUserExample);
    }

    @Override
    public void insertUserMsg(MsgQueue.PushMsgOperator pushMsgOperator) {
        List<MsgQueue.BizMsgUserInfo> userInfos = pushMsgOperator.getUserInfos();
        MsgQueue.MsgInfo msgInfo = pushMsgOperator.getMsgInfo();
        if(userInfos != null && !userInfos.isEmpty() && msgInfo != null) {
            BizMsgUser bizMsgUser = userInfos.get(0).getBizMsgUser();

            String bizMsgThresholdId = msgInfo.getBizMsgThresholdId();
            Collection<String> roleIds = msgInfo.getRoleIds();
            String thresholdSet = msgInfo.getThresholdSet();
            BizMsgSendWithBLOBs bizMsgSend = new BizMsgSendWithBLOBs();
            BeanUtils.copyProperties(bizMsgUser, bizMsgSend);

            String id = CommonUtil.getUUID();
            bizMsgSend.setId(id);
            bizMsgSend.setBizMsgThresholdId(bizMsgThresholdId);
            if (roleIds != null && !roleIds.isEmpty()) {
                bizMsgSend.setRoleId(Joiner.on(",").join(roleIds));
            }
            if (StringUtils.isNotEmpty(thresholdSet)) {
                bizMsgSend.setThresholdSet(thresholdSet);
            }
            int count = 0;
            for (int i = 0, len = userInfos.size(); i <  len; i++) {
                MsgQueue.BizMsgUserInfo bizMsgUserInfo = userInfos.get(i);
                bizMsgUser = bizMsgUserInfo.getBizMsgUser();
                bizMsgUser.setBizMsgSendId(id);
                boolean isValid = bizMsgUserInfo.isValid();
                if(isValid) {
                    boolean flag =bizMsgUserInfo.getPushMsgTemplate().valiIsPush(bizMsgUserInfo.getValiMap());
                    if(flag) {
                        LOGGER.info("当前需要校验，并且校验的结果为：" + flag);
                        continue;
                    }
                }
                count += bizMsgUserMapper.insertSelective(bizMsgUser);
            }
            if(count > 0) { //当有用户消息插入时，维护这张表的数据
                bizMsgSendMapper.insertSelective(bizMsgSend);
            }
        }
    }
}